clear all
#delimit ;
version 11.2;
set mem 650m;
set matsize 800;
set maxvar 32767;
set more off;
cap log close;
set seed 98056;


cd "H:\Mi unidad\Grant_new\EFF";

use sample_eff.dta, clear;



global results /**/dif difchar difcoef edif edifchar edifcoef odif odifchar odifcoef difcons  /*idif idifchar*/ idifcoef /*wdif wdifchar*/ wdifcoef /*ldif ldifchar*/ ldifcoef rdifcoef;

forvalues x=1(1)5{;
global rhsage age1_imp`x' age2_imp`x' age3_imp`x' /*age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x'*/;
*global rhsage age_imp`x' agesq_imp`x';
global rhshh hh2_imp`x' hh3_imp`x' hh4_imp`x' hh5_imp`x' /*hh6_imp`x'*/;
*global rhshh hhincome_imp`x' hhincomesq_imp`x';
*global rhshh lincome_imp`x';
global rhsnw w2_imp`x' w3_imp`x' w4_imp`x' w5_imp`x';
*global rhsnw w_imp`x';

global housingdebts busloan_imp`x' credcard_imp`x' restbis_imp`x';
global busloandebts housing_imp`x' credcard_imp`x' restbis_imp`x';
global credcarddebts housing_imp`x' busloan_imp`x' restbis_imp`x';
global restbisdebts housing_imp`x' busloan_imp`x' credcard_imp`x';

gen risk_imp`x'= riskaver_imp`x'<=2;

drop if oth_inact_imp`x'==1 | disable_imp`x'==1 | student_imp`x'==1 | housew_imp`x'==1;
keep if age_imp`x'>=3 & age_imp`x' <=7.5;
drop if madult_imp`x'==1;

cap drop age1_imp`x' age2_imp`x' age3_imp`x' age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x' age9_imp`x';

gen age1_imp`x'=0;
gen age2_imp`x'=0;
gen age3_imp`x'=0;
gen age4_imp`x'=0;

replace age1_imp`x'=1 if age_imp`x'>=3 & age_imp`x'<4.3;

replace age2_imp`x'=1 if age_imp`x'>=4.3 & age_imp`x'<5.5;

replace age3_imp`x'=1 if age_imp`x'>=5.5 & age_imp`x'<6.5;

replace age4_imp`x'=1 if age_imp`x'>=6.5;

replace hh5_imp`x'=1 if hh6_imp`x'==1;

*gen risk_imp`x'= riskaver_imp`x'<=2;
};
saveold sample_boot, replace /*version(11.2)*/;

*/;*/;
*log using "G:\Mi unidad\Grant_new\Results\bootstrap\prev\1risk2age\ALL.smcl", replace;
foreach ref in 2002 /*2005 2008*/ /*2011 2014*/{;
local refp=`ref'+6;
foreach var in /*busloan*/ housing restbis  /*credcard*/{;



postfile buffer_`var' meanhat using sampmean_b, replace;
forvalues i=1/100{;
cap scalar drop localx;
scalar localx=int(1 + 5 * uniform());
local x=scalar(localx);
global rhsage age1_imp`x' age2_imp`x' age3_imp`x' /*age4_imp`x' age5_imp`x' age6_imp`x' age7_imp`x' age8_imp`x'*/;
*global rhsage age_imp`x' agesq_imp`x';
global rhshh hh2_imp`x' hh3_imp`x' hh4_imp`x' hh5_imp`x'/* hh6_imp`x'*/;
*global rhshh hhincome_imp`x' hhincomesq_imp`x';
*global rhshh lincome_imp`x';
global rhsnw w2_imp`x' w3_imp`x' w4_imp`x' w5_imp`x'/**/;
*global rhsnw w_imp`x';

*cap preserve;
use sample_boot, clear;
svyset [pw=facine3_imp`x'];
cap rename `var'w1_imp`x' w1_imp`x';
cap rename `var'w2_imp`x' w2_imp`x';
cap rename `var'w3_imp`x' w3_imp`x';
cap rename `var'w4_imp`x' w4_imp`x';
cap rename `var'w5_imp`x' w5_imp`x';

cap rename l`var'w_imp`x' w_imp`x';
cap drop ysample;
reg l`var'_imp`x' $rhsage $rhshh  $rhsnw univ_imp`x' bhealth_imp`x' unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' 
	sex_imp`x' couplewith_imp`x' couplewithout_imp`x' singlewith_imp`x' risk_imp`x' ration2_imp`x' /*risklot_imp`x' riskfair_imp`x' risksome_imp`x'*//*, ll(0) $`var'debts if year==`y'*/;
	

************************************;
gen ysample=.;
forvalues t=2002(6)2008{;
replace ysample=`t' if e(sample)==1 & year==`t';
};
************************************;
	
scalar nsample_`var'_`ref'=e(N);

bsample /*scalar(nsample_`var'_`ref')*/, strata(ysample);
*bsample scalar(nsample_`var'_`ref') if e(N);	

foreach y in `ref' `refp'/**/{;

*cap restore;
cap rename `var'w1_imp`x' w1_imp`x';
cap rename `var'w2_imp`x' w2_imp`x';
cap rename `var'w3_imp`x' w3_imp`x';
cap rename `var'w4_imp`x' w4_imp`x';
cap rename `var'w5_imp`x' w5_imp`x';

cap rename l`var'w_imp`x' w_imp`x';




global housingdebts busloan_imp`x' credcard_imp`x' restbis_imp`x';
global busloandebts housing_imp`x' credcard_imp`x' restbis_imp`x';
global credcarddebts housing_imp`x' busloan_imp`x' restbis_imp`x';
global restbisdebts housing_imp`x' busloan_imp`x' credcard_imp`x';

*svyset [pw=facine3_imp`x'];
tab `var'_imp`x' if year==`y';
cap noisily svy: tobit l`var'_imp`x' $rhsage $rhshh  $rhsnw univ_imp`x' bhealth_imp`x' unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' 
	sex_imp`x' couplewith_imp`x' couplewithout_imp`x' singlewith_imp`x' risk_imp`x' ration2_imp`x' /*risklot_imp`x' riskfair_imp`x' risksome_imp`x'*/ /*$`var'debts */if year==`y', ll(0);
scalar index`y'_`var'=.;
if _rc==0 {;
cap drop scalar(index`y'_`var');
scalar index`y'_`var'=1;
};
cap noisily matrix coefs_`var'`y'_imp`x'=e(b);
cap noisily scalar co_constant`var'_`y'_imp`x'=coefs_`var'`y'_imp`x'[1,23];
cap noisily scalar sigma`var'_`y'_imp`x'=coefs_`var'`y'_imp`x'[1,24];


}/*foreach y in 2005 2008 2011 2014 2017/**/{;*/;

foreach y in `ref' `refp'/**/ {;
foreach a in `ref' `refp'/**/ {;
*use sample_eff_imp`x'.dta, clear;
*svy: probit `var'_imp`x' $rhsage $rhshh  $rhsnw univ_imp`x' bhealth_imp`x' unemployed_imp`x' retiree_imp`x' selfemployed_imp`x' sex_imp`x' couplewith_imp`x' 
	*couplewithout_imp`x' singlewith_imp`x' risklot_imp`x' riskfair_imp`x' risksome_imp`x' $`var'debts if year==`y';

*keep if e(sample);
*cap noisily scalar age_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,1];
*cap noisily scalar agesq_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,2];
cap noisily scalar age1_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,1];
cap noisily scalar age2_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,2];
cap noisily scalar age3_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,3];
/*cap noisily scalar age4_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,4];
cap noisily scalar age5_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,5];
cap noisily scalar age6_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,6];
cap noisily scalar age7_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,7];
cap noisily scalar age8_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,8];
*/cap noisily scalar hh2_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,4];
cap noisily scalar hh3_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,5];
cap noisily scalar hh4_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,6];
cap noisily scalar hh5_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,7];
*cap noisily scalar hh6_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,8];
cap noisily scalar w2_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,8];
cap noisily scalar w3_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,9];
cap noisily scalar w4_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,10];
cap noisily scalar w5_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,11];
cap noisily scalar univ_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,12];
cap noisily scalar bhealth_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,13];
cap noisily scalar unemployed_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,14];
cap noisily scalar retiree_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,15];
cap noisily scalar selfemployed_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,16];
cap noisily scalar sex_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,17];
cap noisily scalar couplewith_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,18];
cap noisily scalar couplewithout_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,19];
cap noisily scalar singlewith_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,20];
*cap noisily scalar risklot_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,20];
*cap noisily scalar riskfair_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,21];
*cap noisily scalar risksome_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,22];
cap noisily scalar risk_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,21];
cap noisily scalar ration2_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,22];
*scalar bankjob_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,30];
*scalar debt1_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,31];
*scalar debt2_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,32];
*scalar debt3_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,33];
cap noisily scalar constant_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,23];
cap noisily scalar sigma_`var'_imp`x'=coefs_`var'`a'_imp`x'[1,24];

gen inst_imp`x'="`var'";

if inst_imp`x'!="housing" rename housing_imp`x' debt1_imp`x';
if inst_imp`x'=="housing" rename busloan_imp`x' debt1_imp`x';
if inst_imp`x'=="housing" rename credcard_imp`x' debt2_imp`x';
if inst_imp`x'=="busloan" rename credcard_imp`x' debt2_imp`x';
if inst_imp`x'=="credcard" rename busloan_imp`x' debt2_imp`x';
if inst_imp`x'=="restbis" rename busloan_imp`x' debt2_imp`x';
if inst_imp`x'!="restbis" rename restbis_imp`x' debt3_imp`x';
if inst_imp`x'=="restbis" rename credcard_imp`x' debt3_imp`x';


cap noisily gen `var'`y'_`a'_imp`x'=/*scalar(age_`var'_imp`x')*age_imp`x'+scalar(agesq_`var'_imp`x')*agesq_imp`x'*/scalar(age1_`var'_imp`x')*age1_imp`x'+scalar(age2_`var'_imp`x')*age2_imp`x'
+scalar(age3_`var'_imp`x')*age3_imp`x'/*+scalar(age4_`var'_imp`x')*age4_imp`x'+scalar(age5_`var'_imp`x')*age5_imp`x'+
scalar(age6_`var'_imp`x')*age6_imp`x'+scalar(age7_`var'_imp`x')*age7_imp`x'+scalar(age8_`var'_imp`x')*age8_imp`x'*/+scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
*/+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'/**/+scalar(univ_`var'_imp`x')*univ_imp`x'+scalar(bhealth_`var'_imp`x')*bhealth_imp`x'
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+scalar(sex_`var'_imp`x')*sex_imp`x'+scalar(couplewith_`var'_imp`x')*couplewith_imp`x'+scalar(couplewithout_`var'_imp`x')*couplewithout_imp`x'
+scalar(singlewith_`var'_imp`x')*singlewith_imp`x' +scalar(risk_`var'_imp`x')*risk_imp`x' +scalar(ration2_`var'_imp`x')*ration2_imp`x'+ scalar(co_constant`var'_`a'_imp`x') if year==`y';


cap noisily gen `var'_coefs`a'_char`y'_i`x'=`var'`y'_`a'_imp`x';
cap noisily gen nc`var'_coefs`a'_char`y'_i`x'=normal((`var'`y'_`a'_imp`x')/scalar(sigma`var'_`y'_imp`x'));
cap noisily gen nd`var'_coefs`a'_char`y'_i`x'=normalden((`var'`y'_`a'_imp`x')/scalar(sigma`var'_`y'_imp`x'));

** Constant **;
cap noisily gen co`var'`y'_`a'_imp`x'=/*scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'+/*scalar(hh6_`var'_imp`x')*hh6_imp`x'
+*/scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
/*+scalar(w5_`var'_imp`x')*w5_imp`x'*/
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+ */scalar(co_constant`var'_`a'_imp`x')/**/ if year==`y';

** Income variables **;
cap noisily gen i`var'`y'_`a'_imp`x'=/**/scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
*//*+scalar(w5_`var'_imp`x')*w5_imp`x'
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+ scalar(co_constant`var'_`a'_imp`x')*//**/ if year==`y';

** Rest Income variables **;
cap noisily gen ri`var'`y'_`a'_imp`x'=/*scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'+scalar(hh6_`var'_imp`x')*hh6_imp`x'
+*/scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'/**/+scalar(ration2_`var'_imp`x')*ration2_imp`x'
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+scalar(age1_`var'_imp`x')*age1_imp`x'+scalar(age2_`var'_imp`x')*age2_imp`x'
+scalar(age3_`var'_imp`x')*age3_imp`x'+scalar(univ_`var'_imp`x')*univ_imp`x'
+scalar(bhealth_`var'_imp`x')*bhealth_imp`x'+scalar(sex_`var'_imp`x')*sex_imp`x'+scalar(couplewith_`var'_imp`x')*couplewith_imp`x'+scalar(couplewithout_`var'_imp`x')*couplewithout_imp`x'
+scalar(singlewith_`var'_imp`x')*singlewith_imp`x' +scalar(risk_`var'_imp`x')*risk_imp`x'+ scalar(co_constant`var'_`a'_imp`x')/**/ if year==`y';

** Wealth variables **;
cap noisily gen w`var'`y'_`a'_imp`x'=/*scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'+scalar(hh6_`var'_imp`x')*hh6_imp`x'
+*/scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'/*
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+ scalar(co_constant`var'_`a'_imp`x')*//**/ if year==`y';

** Rest Wealth variables **;
cap noisily gen rw`var'`y'_`a'_imp`x'=/**/scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'*/+scalar(ration2_`var'_imp`x')*ration2_imp`x'
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+scalar(age1_`var'_imp`x')*age1_imp`x'+scalar(age2_`var'_imp`x')*age2_imp`x'
+scalar(age3_`var'_imp`x')*age3_imp`x'+scalar(univ_`var'_imp`x')*univ_imp`x'
+scalar(bhealth_`var'_imp`x')*bhealth_imp`x'+scalar(sex_`var'_imp`x')*sex_imp`x'+scalar(couplewith_`var'_imp`x')*couplewith_imp`x'+scalar(couplewithout_`var'_imp`x')*couplewithout_imp`x'
+scalar(singlewith_`var'_imp`x')*singlewith_imp`x' +scalar(risk_`var'_imp`x')*risk_imp`x'+ scalar(co_constant`var'_`a'_imp`x')/**//**/ if year==`y';

** Labour variables **;
cap noisily gen l`var'`y'_`a'_imp`x'=/*scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'+scalar(hh6_`var'_imp`x')*hh6_imp`x'
+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'*/
/*+scalar(w5_`var'_imp`x')*w5_imp`x'
+*/scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
/*+ scalar(co_constant`var'_`a'_imp`x')*/ if year==`y';

** Rest Labour variables **;
cap noisily gen rl`var'`y'_`a'_imp`x'=/**/scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
*/+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'+scalar(ration2_`var'_imp`x')*ration2_imp`x'
/*+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
*/+scalar(age1_`var'_imp`x')*age1_imp`x'+scalar(age2_`var'_imp`x')*age2_imp`x'
+scalar(age3_`var'_imp`x')*age3_imp`x'+scalar(univ_`var'_imp`x')*univ_imp`x'
+scalar(bhealth_`var'_imp`x')*bhealth_imp`x'+scalar(sex_`var'_imp`x')*sex_imp`x'+scalar(couplewith_`var'_imp`x')*couplewith_imp`x'+scalar(couplewithout_`var'_imp`x')*couplewithout_imp`x'
+scalar(singlewith_`var'_imp`x')*singlewith_imp`x' +scalar(risk_`var'_imp`x')*risk_imp`x'+ scalar(co_constant`var'_`a'_imp`x') if year==`y';

** Ration variables **;
cap noisily gen r`var'`y'_`a'_imp`x'=scalar(ration2_`var'_imp`x')*ration2_imp`x' /*scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'+scalar(hh6_`var'_imp`x')*hh6_imp`x'
+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'*/
/*+scalar(w5_`var'_imp`x')*w5_imp`x'
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'*/
/*+ scalar(co_constant`var'_`a'_imp`x')*/ if year==`y';

** Rest Ration variables **;
cap noisily gen rr`var'`y'_`a'_imp`x'=/*scalar(ration2_`var'_imp`x')*ration2_imp`x'*/scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
*/+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
+scalar(age1_`var'_imp`x')*age1_imp`x'+scalar(age2_`var'_imp`x')*age2_imp`x'
+scalar(age3_`var'_imp`x')*age3_imp`x'+scalar(univ_`var'_imp`x')*univ_imp`x'
+scalar(bhealth_`var'_imp`x')*bhealth_imp`x'+scalar(sex_`var'_imp`x')*sex_imp`x'+scalar(couplewith_`var'_imp`x')*couplewith_imp`x'+scalar(couplewithout_`var'_imp`x')*couplewithout_imp`x'
+scalar(singlewith_`var'_imp`x')*singlewith_imp`x' +scalar(risk_`var'_imp`x')*risk_imp`x'+ scalar(co_constant`var'_`a'_imp`x') if year==`y';

** Economic variables **;
cap noisily gen ec`var'`y'_`a'_imp`x'=/**/scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
*/+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'+scalar(ration2_`var'_imp`x')*ration2_imp`x'/**/
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
/**/+ scalar(co_constant`var'_`a'_imp`x') if year==`y';

** Economic variables (not included constant)**;
cap noisily gen e2`var'`y'_`a'_imp`x'=/**/scalar(hh2_`var'_imp`x')*hh2_imp`x' 
+scalar(hh3_`var'_imp`x')*hh3_imp`x'+scalar(hh4_`var'_imp`x')*hh4_imp`x'+scalar(hh5_`var'_imp`x')*hh5_imp`x'/*+scalar(hh6_`var'_imp`x')*hh6_imp`x'
*/+scalar(w2_`var'_imp`x')*w2_imp`x'+scalar(w3_`var'_imp`x')*w3_imp`x'+scalar(w4_`var'_imp`x')*w4_imp`x'
+scalar(w5_`var'_imp`x')*w5_imp`x'+scalar(ration2_`var'_imp`x')*ration2_imp`x'/**/
+scalar(unemployed_`var'_imp`x')*unemployed_imp`x'+scalar(retiree_`var'_imp`x')*retiree_imp`x'+scalar(selfemployed_`var'_imp`x')*selfemployed_imp`x'
/*+ scalar(co_constant`var'_`a'_imp`x')*/ if year==`y';


** Family variables **;
cap noisily gen o`var'`y'_`a'_imp`x'=/*scalar(age_`var'_imp`x')*age_imp`x'+scalar(agesq_`var'_imp`x')*agesq_imp`x'*/scalar(age1_`var'_imp`x')*age1_imp`x'+scalar(age2_`var'_imp`x')*age2_imp`x'
+scalar(age3_`var'_imp`x')*age3_imp`x'/*+scalar(age4_`var'_imp`x')*age4_imp`x'+scalar(age5_`var'_imp`x')*age5_imp`x'+
scalar(age6_`var'_imp`x')*age6_imp`x'+scalar(age7_`var'_imp`x')*age7_imp`x'+scalar(age8_`var'_imp`x')*age8_imp`x'*/+scalar(univ_`var'_imp`x')*univ_imp`x'
+scalar(bhealth_`var'_imp`x')*bhealth_imp`x'+scalar(sex_`var'_imp`x')*sex_imp`x'+scalar(couplewith_`var'_imp`x')*couplewith_imp`x'+scalar(couplewithout_`var'_imp`x')*couplewithout_imp`x'
+scalar(singlewith_`var'_imp`x')*singlewith_imp`x' +scalar(risk_`var'_imp`x')*risk_imp`x'/*+scalar(risklot_`var'_imp`x')*risklot_imp`x' +scalar(riskfair_`var'_imp`x')*riskfair_imp`x'
 +scalar(risksome_`var'_imp`x')*risksome_imp`x'*//*+ scalar(constant_`var'_imp`x')*/ if year==`y';

/* Unconditional mean */;
*cap noisily gen c`y'_`var'`a'_i`x'=normal((`var'`y'_`a'_imp`x')/scalar(sigma`var'_`y'_imp`x'));
cap noisily gen c`y'_`var'`a'_i`x'=nc`var'_coefs`a'_char`y'_i`x'*(`var'_coefs`a'_char`y'_i`x'+scalar(sigma`var'_`y'_imp`x')*nd`var'_coefs`a'_char`y'_i`x'/nc`var'_coefs`a'_char`y'_i`x');

if inst_imp`x'!="housing" rename debt1_imp`x' housing_imp`x';
if inst_imp`x'=="housing" rename debt1_imp`x' busloan_imp`x';
if inst_imp`x'=="housing" rename debt2_imp`x' credcard_imp`x';
if inst_imp`x'=="busloan" rename debt2_imp`x' credcard_imp`x';
if inst_imp`x'=="credcard" rename debt2_imp`x' busloan_imp`x';
if inst_imp`x'=="restbis" rename debt2_imp`x' busloan_imp`x';
if inst_imp`x'!="restbis" rename debt3_imp`x' restbis_imp`x';
if inst_imp`x'=="restbis" rename debt3_imp`x' credcard_imp`x';

cap drop inst_imp`x';

};
};
*replace l`var'_imp`x'=l`var'_imp`x'*10000;


foreach y in `ref' `refp'/**/ {;
foreach a in `ref' `refp'/**/ {;

cap noisily svy: mean c`y'_`var'`a'_i`x';
cap noisily matrix aux_`y'_`a'_i`x'=e(b);
cap noisily scalar c`y'_`var'`a'_i`x'=aux_`y'_`a'_i`x'[1,1];


*Compute weights , Yun (2004)*;
cap noisily svy: mean `var'`y'_`a'_imp`x';
cap noisily matrix x_`y'_`a'_i`x'=e(b);
cap noisily scalar x`y'_`var'`a'_i`x'=x_`y'_`a'_i`x'[1,1];

*calculations for Weights economic vbles.;
cap noisily svy: mean ec`var'`y'_`a'_imp`x';
cap noisily matrix xe_`y'_`a'_i`x'=e(b);
cap noisily scalar xe`y'_`var'`a'_i`x'=xe_`y'_`a'_i`x'[1,1];

*calculations for Weights economic vbles. (not constant);
cap noisily svy: mean e2`var'`y'_`a'_imp`x';
cap noisily matrix xe2_`y'_`a'_i`x'=e(b);
cap noisily scalar xe2`y'_`var'`a'_i`x'=xe2_`y'_`a'_i`x'[1,1];

*calculations for Weights other vbles.;
cap noisily svy: mean o`var'`y'_`a'_imp`x';
cap noisily matrix xo_`y'_`a'_i`x'=e(b);
cap noisily scalar xo`y'_`var'`a'_i`x'=xo_`y'_`a'_i`x'[1,1];

*calculations for Weights income vbles.;
cap noisily svy: mean i`var'`y'_`a'_imp`x';
cap noisily matrix xi_`y'_`a'_i`x'=e(b);
cap noisily scalar xi`y'_`var'`a'_i`x'=xi_`y'_`a'_i`x'[1,1];

*calculations for Weights wealth vbles.;
cap noisily svy: mean w`var'`y'_`a'_imp`x';
cap noisily matrix xw_`y'_`a'_i`x'=e(b);
cap noisily scalar xw`y'_`var'`a'_i`x'=xw_`y'_`a'_i`x'[1,1];

*calculations for Weights labour vbles.;
cap noisily svy: mean l`var'`y'_`a'_imp`x';
cap noisily matrix xl_`y'_`a'_i`x'=e(b);
cap noisily scalar xl`y'_`var'`a'_i`x'=xl_`y'_`a'_i`x'[1,1];

*calculations for Weights ration vbles.;
cap noisily svy: mean r`var'`y'_`a'_imp`x';
cap noisily matrix xr_`y'_`a'_i`x'=e(b);
cap noisily scalar xr`y'_`var'`a'_i`x'=xr_`y'_`a'_i`x'[1,1];

*calculations for Weights rest income vbles.;
cap noisily svy: mean ri`var'`y'_`a'_imp`x';
cap noisily matrix xri_`y'_`a'_i`x'=e(b);
cap noisily scalar xri`y'_`var'`a'_i`x'=xri_`y'_`a'_i`x'[1,1];

*calculations for Weights rest wealth vbles.;
cap noisily svy: mean rw`var'`y'_`a'_imp`x';
cap noisily matrix xrw_`y'_`a'_i`x'=e(b);
cap noisily scalar xrw`y'_`var'`a'_i`x'=xrw_`y'_`a'_i`x'[1,1];

*calculations for Weights rest labour vbles.;
cap noisily svy: mean rl`var'`y'_`a'_imp`x';
cap noisily matrix xrl_`y'_`a'_i`x'=e(b);
cap noisily scalar xrl`y'_`var'`a'_i`x'=xrl_`y'_`a'_i`x'[1,1];

*calculations for Weights rest ration vbles.;
cap noisily svy: mean rr`var'`y'_`a'_imp`x';
cap noisily matrix xrr_`y'_`a'_i`x'=e(b);
cap noisily scalar xrr`y'_`var'`a'_i`x'=xrr_`y'_`a'_i`x'[1,1];

*calculations for Weights constant vbles.;
cap noisily svy: mean co`var'`y'_`a'_imp`x';
cap noisily matrix xco_`y'_`a'_i`x'=e(b);
cap noisily scalar xco`y'_`var'`a'_i`x'=xco_`y'_`a'_i`x'[1,1];

};

};



foreach k in `ref' `refp'   {;
foreach l in `ref' `refp' {;
*foreach m in /*2005 2008 2011 */2014 2017 {;
foreach n in `ref' `refp'  {;

cap noisily gen `var'ec`k'`l'o`k'`n'_i`x'=ec`var'`k'_`l'_imp`x'+o`var'`k'_`n'_imp`x';
cap noisily gen nc`var'ec`k'`l'o`k'`n'_i`x'=normal((`var'ec`k'`l'o`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'ec`k'`l'o`k'`n'_i`x'=normalden((`var'ec`k'`l'o`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));

cap noisily gen `var'e2`k'`l'o`k'`n'_i`x'=e2`var'`k'_`l'_imp`x'+o`var'`k'_`n'_imp`x';
cap noisily gen nc`var'e2`k'`l'o`k'`n'_i`x'=normal((`var'e2`k'`l'o`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'e2`k'`l'o`k'`n'_i`x'=normalden((`var'e2`k'`l'o`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));

* Income;
cap noisily gen `var'i`k'`l'ri`k'`n'_i`x'=i`var'`k'_`l'_imp`x'+ri`var'`k'_`n'_imp`x';
cap noisily gen nc`var'i`k'`l'ri`k'`n'_i`x'=normal((`var'i`k'`l'ri`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'i`k'`l'ri`k'`n'_i`x'=normalden((`var'i`k'`l'ri`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));

* Wealth;
cap noisily gen `var'w`k'`l'rw`k'`n'_i`x'=w`var'`k'_`l'_imp`x'+rw`var'`k'_`n'_imp`x';
cap noisily gen nc`var'w`k'`l'rw`k'`n'_i`x'=normal((`var'w`k'`l'rw`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'w`k'`l'rw`k'`n'_i`x'=normalden((`var'w`k'`l'rw`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));

* Labour;
cap noisily gen `var'l`k'`l'rl`k'`n'_i`x'=l`var'`k'_`l'_imp`x'+rl`var'`k'_`n'_imp`x';
cap noisily gen nc`var'l`k'`l'rl`k'`n'_i`x'=normal((`var'l`k'`l'rl`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'l`k'`l'rl`k'`n'_i`x'=normalden((`var'l`k'`l'rl`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));

* Ration;
cap noisily gen `var'r`k'`l'rr`k'`n'_i`x'=r`var'`k'_`l'_imp`x'+rr`var'`k'_`n'_imp`x';
cap noisily gen nc`var'r`k'`l'rr`k'`n'_i`x'=normal((`var'r`k'`l'rr`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'r`k'`l'rr`k'`n'_i`x'=normalden((`var'r`k'`l'rr`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
/*;
* Constant;
cap noisily gen `var'co`k'`l'rc`k'`n'_i`x'=co`var'`k'_`l'_imp`x'+rc`var'`k'_`n'_imp`x';
cap noisily gen nc`var'co`k'`l'rc`k'`n'_i`x'=normal((`var'co`k'`l'rc`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen nd`var'co`k'`l'rc`k'`n'_i`x'=normalden((`var'co`k'`l'rc`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
*/;


/* Unconditional mean */;
*gen c`var'ec`k'`l'o`k'`n'_i`x'=normal((`var'ec`k'`l'o`k'`n'_i`x')/scalar(sigma`var'_`k'_imp`x'));
cap noisily gen c`var'ec`k'`l'o`k'`n'_i`x'=nc`var'ec`k'`l'o`k'`n'_i`x'*(`var'ec`k'`l'o`k'`n'_i`x'+scalar(sigma`var'_`k'_imp`x')*nd`var'ec`k'`l'o`k'`n'_i`x'/nc`var'ec`k'`l'o`k'`n'_i`x');

cap noisily gen c`var'e2`k'`l'o`k'`n'_i`x'=nc`var'e2`k'`l'o`k'`n'_i`x'*(`var'e2`k'`l'o`k'`n'_i`x'+scalar(sigma`var'_`k'_imp`x')*nd`var'e2`k'`l'o`k'`n'_i`x'/nc`var'e2`k'`l'o`k'`n'_i`x');

cap noisily gen c`var'i`k'`l'ri`k'`n'_i`x'=nc`var'i`k'`l'ri`k'`n'_i`x'*(`var'i`k'`l'ri`k'`n'_i`x'+scalar(sigma`var'_`k'_imp`x')*nd`var'i`k'`l'ri`k'`n'_i`x'/nc`var'i`k'`l'ri`k'`n'_i`x');

cap noisily gen c`var'w`k'`l'rw`k'`n'_i`x'=nc`var'w`k'`l'rw`k'`n'_i`x'*(`var'w`k'`l'rw`k'`n'_i`x'+scalar(sigma`var'_`k'_imp`x')*nd`var'w`k'`l'rw`k'`n'_i`x'/nc`var'w`k'`l'rw`k'`n'_i`x');

cap noisily gen c`var'l`k'`l'rl`k'`n'_i`x'=nc`var'l`k'`l'rl`k'`n'_i`x'*(`var'l`k'`l'rl`k'`n'_i`x'+scalar(sigma`var'_`k'_imp`x')*nd`var'l`k'`l'rl`k'`n'_i`x'/nc`var'l`k'`l'rl`k'`n'_i`x');

cap noisily gen c`var'r`k'`l'rr`k'`n'_i`x'=nc`var'r`k'`l'rr`k'`n'_i`x'*(`var'r`k'`l'rr`k'`n'_i`x'+scalar(sigma`var'_`k'_imp`x')*nd`var'r`k'`l'rr`k'`n'_i`x'/nc`var'r`k'`l'rr`k'`n'_i`x');

*cap noisily gen c`y'_`var'`a'_i`x'=nc`var'_coefs`a'_char`y'_i`x'*(`var'_coefs`a'_char`y'_i`x'+scalar(sigma_`var'_imp`x')*nd`var'_coefs`a'_char`y'_i`x'/nc`var'_coefs`a'_char`y'_i`x');

svy: mean c`var'ec`k'`l'o`k'`n'_i`x';
matrix aux_ec`k'`l'o`k'`n'_i`x'=e(b);
scalar c`var'ec`k'`l'o`k'`n'_i`x'=aux_ec`k'`l'o`k'`n'_i`x'[1,1];

scalar Nec`k'`l'o`k'`n'_i`x'=e(N_pop);
*************************************************************************;

svy: mean c`var'e2`k'`l'o`k'`n'_i`x';
matrix aux_e2`k'`l'o`k'`n'_i`x'=e(b);
scalar c`var'e2`k'`l'o`k'`n'_i`x'=aux_e2`k'`l'o`k'`n'_i`x'[1,1];

scalar Ne2`k'`l'o`k'`n'_i`x'=e(N_pop);
*************************************************************************;
svy: mean c`var'i`k'`l'ri`k'`n'_i`x';
matrix aux_i`k'`l'ri`k'`n'_i`x'=e(b);
scalar c`var'i`k'`l'ri`k'`n'_i`x'=aux_i`k'`l'ri`k'`n'_i`x'[1,1];

scalar Ni`k'`l'ri`k'`n'_i`x'=e(N_pop);
*************************************************************************;
svy: mean c`var'w`k'`l'rw`k'`n'_i`x';
matrix aux_w`k'`l'rw`k'`n'_i`x'=e(b);
scalar c`var'w`k'`l'rw`k'`n'_i`x'=aux_w`k'`l'rw`k'`n'_i`x'[1,1];

scalar Nw`k'`l'rw`k'`n'_i`x'=e(N_pop);

*************************************************************************;
svy: mean c`var'l`k'`l'rl`k'`n'_i`x';
matrix aux_l`k'`l'rl`k'`n'_i`x'=e(b);
scalar c`var'l`k'`l'rl`k'`n'_i`x'=aux_l`k'`l'rl`k'`n'_i`x'[1,1];

scalar Nl`k'`l'rl`k'`n'_i`x'=e(N_pop);

*************************************************************************;
svy: mean c`var'r`k'`l'rr`k'`n'_i`x';
matrix aux_r`k'`l'rr`k'`n'_i`x'=e(b);
scalar c`var'r`k'`l'rr`k'`n'_i`x'=aux_r`k'`l'rr`k'`n'_i`x'[1,1];

scalar Nr`k'`l'rr`k'`n'_i`x'=e(N_pop);

};
};
*};
};




forvalues p =`ref'(6)`refp' {;
** Constant **;

forvalues q =`ref'(6)`refp' {;


cap noisily scalar c`p'_`q'mean_`var'=aux_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar x`p'_`q'mean_`var'=x_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xe`p'_`q'mean_`var'=xe_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xe2`p'_`q'mean_`var'=xe2_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xi`p'_`q'mean_`var'=xi_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xw`p'_`q'mean_`var'=xw_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xl`p'_`q'mean_`var'=xl_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xr`p'_`q'mean_`var'=xr_`p'_`q'_i`x'[1,1];

*******************************************************;
cap noisily scalar xo`p'_`q'mean_`var'=xo_`p'_`q'_i`x'[1,1];


cap noisily scalar co`p'mean`var'=scalar(co_constant`var'_`p'_imp`x');
/*;
** Alt. 1 **;
*Coefs;
cap noisily scalar f_cfs_`p'_`q'mean_`var'=scalar(c`var'ec`q'`p'o`q'`p'_i`x')-scalar(c`var'ec`q'`p'o`q'`q'_i`x');
cap noisily scalar e_cfs_`p'_`q'mean_`var'=scalar(c`var'ec`q'`p'o`q'`p'_i`x')-scalar(c`var'ec`q'`q'o`q'`p'_i`x');
cap noisily scalar e2_cfs_`p'_`q'mean_`var'=scalar(c`var'e2`q'`p'o`q'`p'_i`x')-scalar(c`var'e2`q'`q'o`q'`p'_i`x');
cap noisily scalar i_cfs_`p'_`q'mean_`var'=scalar(c`var'i`q'`p'ri`q'`p'_i`x')-scalar(c`var'i`q'`q'ri`q'`p'_i`x');
cap noisily scalar w_cfs_`p'_`q'mean_`var'=scalar(c`var'w`q'`p'rw`q'`p'_i`x')-scalar(c`var'w`q'`q'rw`q'`p'_i`x');
cap noisily scalar l_cfs_`p'_`q'mean_`var'=scalar(c`var'l`q'`p'rl`q'`p'_i`x')-scalar(c`var'l`q'`q'rl`q'`p'_i`x');

*Charac.;
*cap noisily scalar f_cha_`p'_`q'mean_`var'=scalar(c`var'ec`p'`p'o`p'`p'_i`x')-scalar(c`var'ec`p'`p'o`q'`p'_i`x');
*cap noisily scalar e_cha_`p'_`q'mean_`var'=scalar(c`var'ec`p'`p'o`q'`p'_i`x')-scalar(c`var'ec`q'`p'o`q'`p'_i`x');
*/;
** Alt. 2 **;
*Coefs;
cap noisily scalar e_cfs_`p'_`q'mean_`var'=scalar(c`var'ec`p'`p'o`p'`p'_i`x')-scalar(c`var'ec`p'`q'o`p'`p'_i`x');
cap noisily scalar f_cfs_`p'_`q'mean_`var'=scalar(c`var'ec`p'`p'o`p'`p'_i`x')-scalar(c`var'ec`p'`p'o`p'`q'_i`x');
cap noisily scalar e2_cfs_`p'_`q'mean_`var'=scalar(c`var'e2`p'`p'o`p'`p'_i`x')-scalar(c`var'e2`p'`q'o`p'`p'_i`x');
cap noisily scalar i_cfs_`p'_`q'mean_`var'=scalar(c`var'i`p'`p'ri`p'`p'_i`x')-scalar(c`var'i`p'`q'ri`p'`p'_i`x');
cap noisily scalar w_cfs_`p'_`q'mean_`var'=scalar(c`var'w`p'`p'rw`p'`p'_i`x')-scalar(c`var'w`p'`q'rw`p'`p'_i`x');
cap noisily scalar l_cfs_`p'_`q'mean_`var'=scalar(c`var'l`p'`p'rl`p'`p'_i`x')-scalar(c`var'l`p'`q'rl`p'`p'_i`x');
cap noisily scalar r_cfs_`p'_`q'mean_`var'=scalar(c`var'r`p'`p'rr`p'`p'_i`x')-scalar(c`var'r`p'`q'rr`p'`p'_i`x');

*Charac;
*cap noisily scalar e_cha_`p'_`q'mean_`var'=scalar(c`var'ec`p'`p'o`p'`p'_i`x')-scalar(c`var'ec`q'`p'o`p'`p'_i`x');
*cap noisily scalar f_cha_`p'_`q'mean_`var'=scalar(c`var'ec`q'`p'o`p'`p'_i`x')-scalar(c`var'ec`q'`p'o`q'`p'_i`x');


};
};


foreach ind in `ref' /*2005(3)2005*/{;
local q=`ind';
local p=`q'+6;
/*;
* Total Difference;

cap noisily scalar dif`p'_`q'_`var'=(scalar(c`p'_`p'mean_`var')-scalar(c`q'_`q'mean_`var'));
/*;
scalar dif`p'_`q'_`var'=.;
if (scalar(c`p'_`p'mean_`var')!=. & scalar(c`q'_`q'mean_`var')!=.) {;
		scalar  drop dif`p'_`q'_`var';
		scalar dif`p'_`q'_`var'=(scalar(c`p'_`p'mean_`var')-scalar(c`q'_`q'mean_`var'));
	};

*/;				
* Characteristics;
cap noisily scalar difchar`p'_`q'_`var'=(scalar(c`p'_`q'mean_`var')-scalar(c`q'_`q'mean_`var'));

* Coefficients;

cap noisily scalar difcoef`p'_`q'_`var'=(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));


**************************;
** Economic variables **;
**************************;
			
* Characteristics;
cap noisily scalar edifchar`p'_`q'_`var'=((scalar(xe`p'_`q'mean_`var')-scalar(xe`q'_`q'mean_`var'))/(scalar(x`p'_`q'mean_`var')-scalar(x`q'_`q'mean_`var')))*(scalar(c`p'_`q'mean_`var')-scalar(c`q'_`q'mean_`var'));
*cap noisily scalar edifchar`p'_`q'_`var'=scalar(e_cha_`p'_`q'mean_`var');

* Coefficients;
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar edifcoef`p'_`q'_`var'=scalar(e_cfs_`p'_`q'mean_`var');

* Total Difference ;
cap noisily scalar edif`p'_`q'_`var'=scalar(edifchar`p'_`q'_`var')+scalar(edifcoef`p'_`q'_`var');

**************************;
**   Other variables    **;
**************************;
			
* Characteristics;
cap noisily scalar odifchar`p'_`q'_`var'=((scalar(xo`p'_`q'mean_`var')-scalar(xo`q'_`q'mean_`var'))/(scalar(x`p'_`q'mean_`var')-scalar(x`q'_`q'mean_`var')))*(scalar(c`p'_`q'mean_`var')-scalar(c`q'_`q'mean_`var'));
*cap noisily scalar odifchar`p'_`q'_`var'=scalar(f_cha_`p'_`q'mean_`var');

* Coefficients;
*cap noisily scalar odifcoef`p'_`q'_`var'=((scalar(xo`p'_`p'mean_`var')-scalar(xo`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar odifcoef`p'_`q'_`var'=scalar(f_cfs_`p'_`q'mean_`var');

* Total Difference ;
cap noisily scalar odif`p'_`q'_`var'=scalar(odifchar`p'_`q'_`var')+scalar(odifcoef`p'_`q'_`var');

*/;
/*;
* Characteristics ALT1;
cap noisily scalar difchar`p'_`q'_`var'=(scalar(c`p'_`p'mean_`var')-scalar(c`q'_`p'mean_`var'));

* Coefficients ALT1;

cap noisily scalar difcoef`p'_`q'_`var'=(scalar(c`q'_`p'mean_`var')-scalar(c`q'_`q'mean_`var'));
*/;
* Characteristics ALT2;
cap noisily scalar difchar`p'_`q'_`var'=(scalar(c`p'_`q'mean_`var')-scalar(c`q'_`q'mean_`var'));

* Coefficients ALT2;

cap noisily scalar difcoef`p'_`q'_`var'=(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));


* Economic Coefficients;
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar edifcoef`p'_`q'_`var'=scalar(e_cfs_`p'_`q'mean_`var');

* Economic Coefficients (no constant);
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar e2difcoef`p'_`q'_`var'=scalar(e2_cfs_`p'_`q'mean_`var');

* Income Coefficients;
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar idifcoef`p'_`q'_`var'=scalar(i_cfs_`p'_`q'mean_`var');

* Wealth Coefficients;
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar wdifcoef`p'_`q'_`var'=scalar(w_cfs_`p'_`q'mean_`var');

* Labour Coefficients;
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar ldifcoef`p'_`q'_`var'=scalar(l_cfs_`p'_`q'mean_`var');

* Ration Coefficients;
*cap noisily scalar edifcoef`p'_`q'_`var'=((scalar(xe`p'_`p'mean_`var')-scalar(xe`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar rdifcoef`p'_`q'_`var'=scalar(r_cfs_`p'_`q'mean_`var');


* Family Coefficients;
*cap noisily scalar odifcoef`p'_`q'_`var'=((scalar(xo`p'_`p'mean_`var')-scalar(xo`p'_`q'mean_`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar odifcoef`p'_`q'_`var'=scalar(f_cfs_`p'_`q'mean_`var');


* Total Coefficients;
*cap noisily scalar difcoef`p'_`q'_`var'=scalar(odifcoef`p'_`q'_`var') + scalar(edifcoef`p'_`q'_`var');

* Total Difference;
cap noisily scalar dif`p'_`q'_`var'=(scalar(c`p'_`p'mean_`var')-scalar(c`q'_`q'mean_`var'));

 * Total Characteristics;
*cap noisily scalar difchar`p'_`q'_`var'=scalar(dif`p'_`q'_`var') - scalar(difcoef`p'_`q'_`var');

* Economic Characteristics;
cap noisily scalar edifchar`p'_`q'_`var'=((scalar(xe`p'_`q'mean_`var')-scalar(xe`q'_`q'mean_`var'))/(scalar(x`p'_`q'mean_`var')-scalar(x`q'_`q'mean_`var')))*(scalar(c`p'_`q'mean_`var')-scalar(c`q'_`q'mean_`var'));

* Family Characteristics;
cap noisily scalar odifchar`p'_`q'_`var'=scalar(difchar`p'_`q'_`var')-scalar(edifchar`p'_`q'_`var');

* Economic Difference ;
cap noisily scalar edif`p'_`q'_`var'=scalar(edifchar`p'_`q'_`var')+scalar(edifcoef`p'_`q'_`var');

* Family Difference ;
cap noisily scalar odif`p'_`q'_`var'=scalar(odifchar`p'_`q'_`var')+scalar(odifcoef`p'_`q'_`var');


**************************;
**      Constant        **;
**************************;
*cap noisily scalar difcons`p'_`q'_`var'=((scalar(co`p'mean`var')-scalar(co`q'mean`var'))/(scalar(x`p'_`p'mean_`var')-scalar(x`p'_`q'mean_`var')))*(scalar(c`p'_`p'mean_`var')-scalar(c`p'_`q'mean_`var'));
cap noisily scalar difcons`p'_`q'_`var'=scalar(difcoef`p'_`q'_`var')-scalar(idifcoef`p'_`q'_`var')-scalar(wdifcoef`p'_`q'_`var')-scalar(ldifcoef`p'_`q'_`var')-scalar(rdifcoef`p'_`q'_`var')-scalar(odifcoef`p'_`q'_`var');
*cap restore;

foreach res in dif difchar difcoef edif edifchar edifcoef odif odifchar odifcoef difcons /*idif idifchar*/ idifcoef /*wdif wdifchar*/ wdifcoef /*ldif ldifchar*/ ldifcoef rdifcoef {;
cap drop `res'`p'_`q'_`var';
cap noisily gen `res'`p'_`q'_`var'=scalar(`res'`p'_`q'_`var')*scalar(index`p'_`var')*scalar(index`q'_`var')/**/;
cap noisily qui sum `res'`p'_`q'_`var' ;
cap noisily post buffer_`var' (r(mean));

} /*foreach res in result*/;

}/* ind*/;
} /* for each iteration */;
postclose buffer_`var';
use sampmean_b, clear;

local j=1;
foreach res in dif difchar difcoef edif edifchar  edifcoef odif odifchar odifcoef difcons  /*idif idifchar*/ idifcoef /*wdif wdifchar*/ wdifcoef /*ldif ldifchar*/ ldifcoef rdifcoef {;
sum if (_n==`j' | _n==`j'+14 | _n==`j'+28    | _n==`j'+42   | _n==`j'+56   | _n==`j'+70   | _n==`j'+84   | _n==`j'+98   | _n==`j'+112  | _n==`j'+126 |
   _n==`j'+140 | _n==`j'+154 | _n==`j'+168   | _n==`j'+182  | _n==`j'+196  | _n==`j'+210  | _n==`j'+224  | _n==`j'+238  | _n==`j'+252  | _n==`j'+266 | 
   _n==`j'+280 | _n==`j'+294 | _n==`j'+308   | _n==`j'+322  | _n==`j'+336  | _n==`j'+350  | _n==`j'+364  | _n==`j'+378  | _n==`j'+392  | _n==`j'+406 | 
   _n==`j'+420 | _n==`j'+434 | _n==`j'+448   | _n==`j'+462  | _n==`j'+476  | _n==`j'+490  | _n==`j'+504  | _n==`j'+518  | _n==`j'+532  | _n==`j'+546 | 
   _n==`j'+560 | _n==`j'+574 | _n==`j'+588   | _n==`j'+602  | _n==`j'+616  | _n==`j'+630  | _n==`j'+644  | _n==`j'+658  | _n==`j'+672  | _n==`j'+686 | 
   _n==`j'+700 | _n==`j'+714 | _n==`j'+728   | _n==`j'+742  | _n==`j'+756  | _n==`j'+770  | _n==`j'+784  | _n==`j'+798  | _n==`j'+812  | _n==`j'+826 |
   _n==`j'+840 | _n==`j'+854 | _n==`j'+868   | _n==`j'+882  | _n==`j'+896  | _n==`j'+910  | _n==`j'+924  | _n==`j'+938  | _n==`j'+952  | _n==`j'+966 |
   _n==`j'+980 | _n==`j'+994 | _n==`j'+1008  | _n==`j'+1022 | _n==`j'+1036 | _n==`j'+1050 | _n==`j'+1064 | _n==`j'+1078 | _n==`j'+1092 | _n==`j'+1106 | 
   _n==`j'+1120 | _n==`j'+1134 | _n==`j'+1148| _n==`j'+1162 | _n==`j'+1176 | _n==`j'+1190 | _n==`j'+1204 | _n==`j'+1218 | _n==`j'+1232 | _n==`j'+1246 | 
  _n==`j'+1260 | _n==`j'+1274 | _n==`j'+1288 | _n==`j'+1302 | _n==`j'+1316 | _n==`j'+1330 | _n==`j'+1344 | _n==`j'+1358 | _n==`j'+1372 | _n==`j'+1386)/**/ & meanhat!=.;

 
local j=`j'+1;
scalar `res'_`var'`p'_`q'=r(mean);
scalar se_`res'_`var'`p'_`q'=r(sd);
scalar N_`res'_`var'`p'_`q'=r(N);
} /*foreach res in result*/;

cap log close;
log using "H:\Mi unidad\Grant_new\SERIEs\2nd_revision\tobit_year_`p'_`q'_`var'.smcl", replace;
foreach res in dif difchar difcoef edif edifchar  edifcoef odif odifchar odifcoef difcons  /*idif idifchar*/ idifcoef /*wdif wdifchar*/ wdifcoef /*ldif ldifchar*/ ldifcoef rdifcoef{;
di "`res'";
di %3.0f scalar(N_`res'_`var'`p'_`q');
di %5.4f scalar(`res'_`var'`p'_`q');
di %5.4f scalar(se_`res'_`var'`p'_`q');
di "";
} /*foreach res in result*/;
cap log close;

*} /*foreach x*/;
} /*foreach var in housing busloan credcard restbis*/;
}/* ref */;
